import Vue from 'vue'
import VueI18n from 'vue-i18n'
import App from './App.vue'
// 路由
import router from './router'
// 数据存储
import store from './store'
// ant ui主题
import Antd from 'ant-design-vue'
import 'ant-design-vue/dist/antd.css'
import axios from 'axios'
import 'jquery'
//注入全局属性$message
import { message, notification } from 'ant-design-vue'

Vue.use(VueI18n)
Vue.use(Antd)

Vue.prototype.$axios = axios
Vue.config.productionTip = false

Vue.prototype.$message = message;
Vue.prototype.$notification = notification;
message.config({
  duration: 2,
});

const i18n = new VueI18n({
  locale: 'zh',
  messages: {
    'zh': require('@/assets/languages/zh.json'),
    'en': require('@/assets/languages/en.json')
  }
});

export let $t = i18n.t

if (window) {
  window.$t = $t.bind(i18n)
}

// 响应拦截器
axios.interceptors.response.use(
  response => {
    // 如果返回的状态码为200，说明接口请求成功，可以正常拿到数据
    // 否则的话抛出错误
    if (response.status === 200) {
      if (response.data && response.data.code && response.data.code == '-1') {
        message.error(response.data.comment + "：" + response.data.message);
      }
      return Promise.resolve(response);
    } else {
      return Promise.reject(response);
    }
  },
  // 服务器状态码不是2开头的的情况
  error => {
    if (error.response && error.response.status) {
      switch (error.response.status) {
        // 404请求不存在
        case 404:
          message.error(window.$t('404'));
          break;
        // 其他错误，直接抛出错误提示
        default:
          message.error(window.$t('error'));
      }
      return Promise.reject(error.response);
    } else {
      message.error(error.toString());
      return Promise.reject(error);
    }
  }
)

new Vue({
  router,
  store,
  i18n,
  message,
  notification,
  render: h => h(App)
}).$mount('#app')
